package com.pj.project.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pj.project.model.pojo.SpGoodsSort;
import com.pj.project.model.vo.SpGoodsSortVO;
import com.pj.project.service.SpGoodsSortService;
import com.pj.project.model.mapper.SpGoodsSortMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author hanwu
* @description 针对表【sp_goods_sort(物品分类表)】的数据库操作Service实现
* @createDate 2023-01-26 11:35:51
*/
@Service
public class SpGoodsSortServiceImpl extends ServiceImpl<SpGoodsSortMapper, SpGoodsSort>
    implements SpGoodsSortService{

    @Resource
    SpGoodsSortMapper goodsSortMapper;

    @Override
    public List<SpGoodsSortVO> listVo() {
        //        创建vo对象
        List<SpGoodsSortVO> spGoodsSortVOList = new ArrayList<>();
        recursivelyFindGoodsSort(spGoodsSortVOList, 0L);
        return spGoodsSortVOList;
    }

    @Override
    public boolean del(Integer id) {
        //        查询仓库是否存在
        if(goodsSortMapper.selectById(id) != null){
//            存在执行删除
            return goodsSortMapper.deleteById(id) != 0;
        }
        return false;
    }

    /**
     * 递归查询
     * @param spGoodsSortVOList  vo
     * @param parentId 父级id
     */
    private void recursivelyFindGoodsSort(List<SpGoodsSortVO> spGoodsSortVOList, Long parentId) {
        //递归获取所有子类别，并组合成为一个“目录树”
        List<SpGoodsSort> goodsSortList = goodsSortMapper.selectGoodsSortByParentId(parentId);
        if(!CollectionUtils.isEmpty(goodsSortList)){
            goodsSortList.forEach(goodsSort->{
                SpGoodsSortVO goodsSortVO = new SpGoodsSortVO();
//                拷贝对象
                BeanUtils.copyProperties(goodsSort, goodsSortVO);
                spGoodsSortVOList.add(goodsSortVO);
                recursivelyFindGoodsSort(goodsSortVO.getChildren(),goodsSortVO.getId());
            });
        }
    }

}




